package org.saratogaschools.edd.firefighter.client.view;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.uibinder.client.UiHandler;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.Widget;

public class EnterNameView extends Composite {

	public interface Presenter {
		void onNameEntered(String name);
	}

	private static EnterNameViewUiBinder uiBinder = GWT
			.create(EnterNameViewUiBinder.class);

	interface EnterNameViewUiBinder extends UiBinder<Widget, EnterNameView> {
	}

	private Presenter presenter;
	
	@UiField
	TextBox nameBox;
	
	@UiField
	Button nextButton;

	public EnterNameView() {
		initWidget(uiBinder.createAndBindUi(this));
	}

	public void setPresenter(Presenter presenter) {
		this.presenter = presenter;
	}
	
	@UiHandler("nextButton")
	void onNextButtonClicked(ClickEvent evt) {
		nextButton.setEnabled(false);
		nameBox.setEnabled(false);
		String name = nameBox.getText();
		if (name.isEmpty() == true) {
			nextButton.setEnabled(true);
			nameBox.setEnabled(true);
			nameBox.setFocus(true);
		}
		else {
			presenter.onNameEntered(name);
		}
	}

}
